part of '../index.dart';

class ConversationCard extends StatelessWidget {
  const ConversationCard(this.conversation, {super.key});
  final EMConversation conversation;

  Widget _buildSubTitle(String? nickname) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 10),
      child: DefaultTextStyle.merge(
        style: const TextStyle(
          fontWeight: FontWeight.w500,
          fontSize: 12,
          color: Color(0xFFC1C1C1),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              nickname?.isNotEmpty == true ? nickname! : 'user',
              style: const TextStyle(fontSize: 16, color: Colors.black),
            ),
            const SizedBox(height: 2),
            FutureBuilder<EMMessage?>(
              future: conversation.latestMessage(),
              builder: (context, snapshot) {
                final lst = ImManager.resolveMessage(snapshot.data) ?? '';
                return Text(lst, maxLines: 1);
              },
            )
          ],
        ),
      ),
    );
  }

  Widget _buildAvatar(String? url) {
    return CachedNetworkImage(
      imageUrl: url?.startsWith('http') == true ? url! : Constants.avatar,
      width: 56,
      height: 56,
      fit: BoxFit.cover,
      fadeInDuration: Duration.zero,
      fadeOutDuration: Duration.zero,
      memCacheWidth: 100,
    ).clipRRect(99);
  }

  void _onRemove(BuildContext context) {
    showDialog(
      context: context,
      barrierColor: const Color(0xFF181818).withValues(alpha: 0.55),
      builder: (context) {
        return _Dialog(conversation.id);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final exclude = [
      ImManager.customerId,
      ImManager.systemNoticeId,
      ImManager.platformNoticeId,
      ImManager.likeId,
      ImManager.commentId,
      ImManager.giftId,
    ];
    if (exclude.contains(conversation.id)) return const SizedBox();
    return Slidable(
      endActionPane: ActionPane(
        motion: const ScrollMotion(),
        children: [
          SlidableAction(
            onPressed: (v) {},
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.white,
            label: '',
          ),
          SlidableAction(
            onPressed: (v) {
              _onRemove(context);
            },
            borderRadius: BorderRadius.horizontal(
              right: Radius.circular(Tools.isAr ? 0 : 10),
              left: Radius.circular(Tools.isAr ? 10 : 0),
            ),
            backgroundColor: const Color(0xFFFE4A49),
            foregroundColor: Colors.white,
            icon: Icons.delete,
          ),
        ],
      ),
      child: Padding(
        padding: const EdgeInsets.only(bottom: 10),
        child: FutureBuilder(
          future: ImManager.of.fetchUserInfoById(conversation.id),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return const SizedBox();
            }
            return Row(
              children: [
                _buildAvatar(snapshot.data?.avatarUrl),
                Expanded(child: _buildSubTitle(snapshot.data?.nickName)),
                FutureBuilder<int>(
                  future: conversation.unreadCount(),
                  builder: (context, snapshot) {
                    Widget? ws;
                    if (snapshot.hasData && snapshot.data! > 0) {
                      ws = Container(
                        width: 20,
                        height: 20,
                        decoration: BoxDecoration(
                          color: const Color(0xFFFF6A6A),
                          borderRadius: BorderRadius.circular(99),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          snapshot.data.toString(),
                          style: const TextStyle(color: Colors.white),
                        ),
                      );
                    }
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        FutureBuilder<EMMessage?>(
                          future: conversation.latestMessage(),
                          builder: (context, snapshot) {
                            final time = Tools.dateFromMS(
                              snapshot.data?.serverTime,
                              'MM-dd',
                            );
                            return Text(
                              time,
                              style: const TextStyle(
                                color: Color(0xFFC1C1C1),
                                fontSize: 12,
                              ),
                            );
                          },
                        ),
                        if (ws != null) ws,
                      ],
                    );
                  },
                ),
              ],
            ).onTap(() {
              context
                  .read<ConversationProvider>()
                  .jumpToChat(conversation, snapshot.data);
            });
          },
        ),
      ),
    );
  }
}
